package com.fudan.backend.interceptor;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
//    @Autowired
//    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object tokenobj = request.getHeader("token");
        //token是否为空
        if(tokenobj == null){
            throw new BaseException(RetCode.NOT_LOGGED_IN.getCode(), RetCode.NOT_LOGGED_IN.getMessage());
        }
        //token是否有效
        String token = tokenobj.toString();
        JwtUtil.checkToken(token);
        //查询Redis数据库是否存在该token
//        String value = redisUtil.get(token);
//        if(value == null){
//            throw new BaseException(RetCode.REDIS_NOT_FOUND.getCode(), RetCode.REDIS_NOT_FOUND.getMessage());
//        }
        return true;
    }
}
